package org.mbds.android.sismapp;

import org.mbds.android.location.GPSTools;
import org.mbds.android.sismapp.activity.AlertActivity;
import org.mbds.android.sismapp.activity.GoogleMapActivity;
import org.mbds.android.sismapp.activity.P2pActivity;
import org.mbds.android.sismapp.activity.SensorChartActivity;
import org.mbds.android.sismapp.activity.SensorDataCollectorActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;


/**
 * Manage menu visibility/disability...
 * Launch action
 * @author VLWB5512
 *
 */
public class SismappMenu {

	public static void onCreateOptionsMenu(Activity context, Menu menu) {
		MenuInflater inflater = context.getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
	}

	public static void onPrepareOptionsMenu(Activity context, Menu menu) {

		menu.findItem(R.id.map).setVisible(true);
		menu.findItem(R.id.sensors).setVisible(true);
		menu.findItem(R.id.p2p).setVisible(true);
		menu.findItem(R.id.graphs).setVisible(true);
		menu.findItem(R.id.gps).setVisible(true);
		menu.findItem(R.id.alert).setVisible(true);
	}

	public static void onOptionsItemSelected(Activity context, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.map:
			map(context);
			break;
		case R.id.gps:
			gps(context);
			break;
		case R.id.graphs:
			graphs(context);
			break;
		case R.id.sensors:
			sensors(context);
			break;
		case R.id.p2p:
			p2p(context);
			break;
		case R.id.alert:
			alert(context.getBaseContext());
			break;
		}
	}
	
	public static void alert(Context context) {
		Intent k = new Intent();
		k.setClass(context, AlertActivity.class);
		k.setAction(Intent.ACTION_VIEW);
		k.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(k);
	}
	public static void p2p(Activity context) {
		Intent k = new Intent();
		k.setClass(context, P2pActivity.class);
		k.setAction(Intent.ACTION_VIEW);
		k.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(k);
	}
	public static void graphs(Activity context) {
		Intent k = new Intent();
		k.setClass(context, SensorChartActivity.class);
		k.setAction(Intent.ACTION_VIEW);
		k.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(k);
	}
	
	public static void sensors(Activity context) {
		Intent k = new Intent();
		k.setClass(context, SensorDataCollectorActivity.class);
		k.setAction(Intent.ACTION_VIEW);
		k.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(k);
	}
	public static void map(Activity context) {
		Intent k = new Intent();
		k.setClass(context, GoogleMapActivity.class);
		k.setAction(Intent.ACTION_VIEW);
		k.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(k);
	}
	
	public static void gps(Activity context) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle("GPS coordinates");
		Location loc = GPSTools.getLastKnownPosition();
		alert.setMessage(GPSTools.getAddress(loc, context));
		alert.setPositiveButton("Ok", null);
		alert.create().show();
	}

}
